For Silverlight programs that get text input, it’s
crucial for the program to be aligned with the hardware keyboard (if one
exists) and the location of that keyboard can’t be anticipated.
Obviously there is more to handling orientation changes than just setting the SupportedOrientations
property! In some cases, you might want to manipulate your layout from
code in the page class. If you need to perform any special handling,
both PhoneApplicationFrame and PhoneApplicationPage include OrientationChangedPhoneApplicationPage supplements that event with a convenient and equivalent protected overridable method called OnOrientationChanged. events.
The MainPage class in the SilverlightOrientationDisplay project shows how to override OnOrientationChanged, but what it does with this information is merely to display the current orientation. The content grid in this project contains a simple TextBlock:
Example 1. SilverlightProject: SilverlightOrientationDisplay File: MainPage.xaml (excerpt)
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <TextBlock Name="txtblk" HorizontalAlignment="Center" VerticalAlignment="Center" /> </Grid>
|
Here’s the complete code-behind file. The constructor initializes the TextBlock text with the current value of the Orientation property, which is a member of the PageOrientation enumeration:
Example 2. SilverlightProject: SilverlightOrientationDisplay File: MainPage.xaml.cs
using System.Windows.Controls; using Microsoft.Phone.Controls;
namespace SilverlightOrientationDisplay { public partial class MainPage : PhoneApplicationPage { public MainPage() { InitializeComponent(); txtblk.Text = Orientation.ToString(); }
protected override void OnOrientationChanged(OrientationChangedEventArgs args) { txtblk.Text = args.Orientation.ToString(); base.OnOrientationChanged(args); } } }
|
The OnOrientationChanged method obtains the new value from the event arguments.